package com.wuyou.mid;


import java.util.*;
import java.util.stream.Collectors;

/**
 * 1233. 删除子文件夹
 */
public class RemoveSubfolders {

    public static void main(String[] args) {
        String[][] folder = {
                {"/a/b/c", "/a/b/ca", "/a/b/d"},
                {"/abc/de", "/abc/dee", "/abc/def", "/abc/def/gh", "/abc/def/ghi", "/abc/def/ghij", "/abc/def/ghijk", "/abc/dez"},
                {"/ah/al/am", "/ah/al"},
                {"/a", "/a/b", "/c/d", "/c/d/e", "/c/f"}
        };

        for (String[] f : folder) {
            System.out.println(solution_2(f));
        }
    }


    public static List<String> solution_2(String[] folder) {
        Map<String, Integer> map = new HashMap<>();
        List<String> resList = new ArrayList<>();
        for(int i = 0; i < folder.length; i ++){
            map.put(folder[i], i);
        }

        for (int i = 0; i < folder.length; i++) {
            String str = folder[i];
            int index = str.length() - 1;
            boolean flag = true;
            for (; index > 1; index--) {
                if(str.charAt(index) == '/'){
                    if (map.get(str.substring(0, index)) != null) {
                        flag = false;
                    }
                }
            }
            if(flag) resList.add(str);
        }
        return resList;
    }


    public static List<String> solution_1(String[] folder) {
        Arrays.sort(folder);
        List<Integer> indexList = new ArrayList<>();
        for (int i = 0; i < folder.length; i++) {
            String temp = folder[i];
            if (!"".equals(temp)) {
                for (int j = i + 1; j < folder.length; j++) {
                    String temp1 = folder[j];
                    if (!"".equals(temp1) && temp1.startsWith(temp + "/")) {
                        folder[j] = "";
                    }
                }
            }
        }
        return Arrays.stream(folder).filter(e -> !"".equals(e)).collect(Collectors.toList());
    }
}
